<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="user.username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="user.pwd"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[
            { required: true, message: '请填写密码' },
            {
              pattern: /^\w{4,8}$/,
              message: '4到8位字母下划线数字组成',
            },
          ]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit"> 提交 </van-button>
        <van-button @click="router.push('/login')" round block type="success">
          直接登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts" name="UserLogin">
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { userRegister } from "@a/index";
import { showToast } from "vant";
const router = useRouter();

const user = reactive({
  username: "",
  pwd: "",
});
// 点击注册
const onSubmit = () => {
  userRegister(user).then((res) => {
    if (res.data.code === 0) {
      showToast({
        type: "success",
        message: "注册成功",
        onClose: () => {
          router.replace("/login");
        },
      });
    }
  });
};
</script>

<style scoped></style>
